/*
 * InputProviderPredicate.java
 *
 * Stateroom - Java framework library for finite state machines
 * Copyright (C) 2009 by Matthew Werny
 * All Rights Reserved
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */
package net.iceflow.stateroom.predicates;

import net.iceflow.stateroom.Utility;
import net.iceflow.stateroom.interfaces.Context;
import net.iceflow.stateroom.interfaces.InputProvider;
import net.iceflow.stateroom.interfaces.Predicate;
import net.iceflow.stateroom.models.InputProviderModel;

/**
 * Predicate that evaluates the truth of the next object from the input provider matching the match reference object.
 * @param <T> Type used to represent the input alphabet for the input provider
 */
public class InputProviderPredicate<T> implements Predicate<InputProviderModel<T>> {
	
	/**
	 * Reference object used to determine truth of the current object from the input provider being a match for this instance.
	 */
	private final T _match;
	
	/**
	 * Constructs a new InputProviderPredicate containing the provided match reference object.
	 * @param match Reference object used for comparison against input objects from the input provider
	 */
	public InputProviderPredicate(final T match) {
		Utility.validateNotNull(match, "match may not be null.");
		
		this._match = match;
	}
	
	/**
	 * Getter for the match field.
	 * @return the match
	 */
	public final T getMatch() {
		return this._match;
	}
	
	/**
	 * Indicates whether the current object available from the input provider matches the reference object.
	 * @param context Machine context from which to determine if the predicate condition is met
	 * @return truth of whether a match exists
	 */
	@Override
	public final boolean isMet(final Context<InputProviderModel<T>> context) {
		final InputProvider<T> provider = context.getModel().getProvider();
		final T nextValue = provider.peekNext();

                boolean truthOfMatch = false;
		
		if (this._match.equals(nextValue)) {
			provider.pollNext();
			truthOfMatch = true;
		}
		
		return truthOfMatch;
	}
}
